package com.yixun.controller;

import com.yixun.entity.Dict;
import com.yixun.entity.DictCategory;
import com.yixun.service.DictCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dictCategory")
public class DictCategoryController extends BaseController {

    @Autowired
    private DictCategoryService dictCategoryService;

    @RequestMapping(value = "/list")
    public String list(){
        return "org/dict/dictCategoryList";
    }

    /**
     * 获取字典分类列表
     * @return
     */
    @RequestMapping(value = "/getDictCategoryList")
    @ResponseBody
    public List<DictCategory> getDictCategoryList(){
        return dictCategoryService.getDictCategoryList();
    }

    @RequestMapping(value = "/go_dictCate_add")
    public String go_dictCate_add(String dictCate_id, Model model){
        model.addAttribute("dictCate_id", dictCate_id);
        return "org/dict/dictCate_add";
    }

    /**
     * 添加数据字典分类
     * @param dictCategory
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody DictCategory dictCategory){
        int row = dictCategoryService.insertDictCate(this.getSessionUser(), dictCategory);
        if (row < 1) {
            return false;
        }
        return true;
    }

    /**
     * 根据id获取数据字典分类
     * @param id
     * @return
     */
    @RequestMapping(value = "/getDictCategory/{id}")
    @ResponseBody
    public DictCategory getDictCategory(@PathVariable String id){
        return dictCategoryService.getDictCategory(id);
    }

    /**
     * 更新数据字典分类
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public boolean edit(@RequestBody DictCategory dictCategory){
        int row = dictCategoryService.updateDictCate(this.getSessionUser(), dictCategory);
        if (row < 1) {
            return false;
        }
        return true;
    }

    /**
     * 删除数据字典分类
     * @param map
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public boolean del(@RequestBody Map<String, String> map){
        int row = dictCategoryService.delDictCate(this.getSessionUser(), map);
        if (row < 1) {
            return false;
        }
        return true;
    }

    /**
     * 数据字典分类保存排序
     * @param map
     * @return
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST)
    @ResponseBody
    public boolean sort(@RequestBody Map<String, String> map){
        int row = dictCategoryService.sortDictCate(this.getSessionUser(), map);
        if (row < 1) {
            return false;
        }
        return true;
    }

    @RequestMapping(value = "/dictList")
    public String dictList(){
        return "org/dict/dictList";
    }

    /**
     * 获取数据字典列表
     * @return
     */
    @RequestMapping(value = "/getDictList/{dict_cat_id}")
    @ResponseBody
    public List<Dict> getDictList(@PathVariable String dict_cat_id){
        return dictCategoryService.getDictList(dict_cat_id);
    }

    @RequestMapping(value = "/go_dict_add")
    public String go_dict_add(String dict_cat_id, String id, Model model){
        model.addAttribute("dict_cat_id", dict_cat_id);
        model.addAttribute("id", id);
        return "org/dict/dict_add";
    }

    /**
     * 添加数据字典
     * @param dict
     * @return
     */
    @RequestMapping(value = "/addDict", method = RequestMethod.POST)
    @ResponseBody
    public boolean addDict(@RequestBody Dict dict){
        int row = dictCategoryService.insertDict(this.getSessionUser(), dict);
        if (row < 1) {
            return false;
        }
        return true;
    }

    /**
     * 根据id获取数据字典
     * @param id
     * @return
     */
    @RequestMapping(value = "/getDictBean/{id}")
    @ResponseBody
    public Dict getDictBean(@PathVariable String id){
        return dictCategoryService.getDictBean(id);
    }

    /**
     * 更新数据字典
     * @param dict
     * @return
     */
    @RequestMapping(value = "/editDict", method = RequestMethod.POST)
    @ResponseBody
    public boolean editDict(@RequestBody Dict dict){
        int row = dictCategoryService.updateDict(this.getSessionUser(), dict);
        if (row < 1) {
            return false;
        }
        return true;
    }

    /**
     * 删除数据字典
     * @param map
     * @return
     */
    @RequestMapping(value = "/delDict", method = RequestMethod.POST)
    @ResponseBody
    public boolean delDict(@RequestBody Map<String, String> map){
        int row = dictCategoryService.delDict(this.getSessionUser(), map);
        if (row < 1) {
            return false;
        }
        return true;
    }

    /**
     * 数据字典保存排序
     * @param map
     * @return
     */
    @RequestMapping(value = "/sortDict", method = RequestMethod.POST)
    @ResponseBody
    public boolean sortDict(@RequestBody Map<String, String> map){
        int row = dictCategoryService.sortDict(this.getSessionUser(), map);
        if (row < 1) {
            return false;
        }
        return true;
    }


}
